package thread;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
//定时任务
public class TimerTaskExample {
    public static void main(String[] args) {
        Timer timer = new Timer();

        // 延迟执行一次任务
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Timer任务执行于: " + new Date());
            }
        }, 5000);

        // 定期执行任务
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                System.out.println("定期Timer任务执行于: " + new Date());
            }
        }, 0, 2000);
    }
}
